import config from '@/common/js/config.js';

const http = ({
	url = '', //请求地址 string
	param = {}, // 请求参数 obj
	method = 'get', //请求方式 string
	contentType, //请求头 obj
	isPermissions = false, //是否认证
} = {}) => {
	// console.log('getUrl(url)',getUrl(url))
	if(getUrl(url) !=`${config.BaseUrl}yang/oversuggestion.do` ){
		if(getUrl(url) !=`${config.BaseUrl}test/wxcheckpay.do` &&  getUrl(url) !=`${config.BaseUrl}examination/consumerinfo.do` ){
			uni.showLoading({
				title: '加载中'
			});
		}
		
	}
	let header = {
		"content-type": contentType ? contentType : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json',
	}
	
	let timeStart = Date.now(); 
	let p = uni.getStorageSync('timing');
	return new Promise((resolve, reject) => {
		uni.request({
			url: getUrl(url),
			data:p?'':json2Form(param),
			header: header,
			method: method,
			complete: (res) => {
				uni.hideLoading();
				console.log(`请求地址:${getUrl(url)}`);
				console.log(`请求参数:${JSON.stringify(json2Form(param))}`);
				console.log(`耗时${Date.now() - timeStart}ms`);
				console.log(`请求头:${JSON.stringify(header)}`);
				if (res.statusCode >= 200 && res.statusCode < 300) {
					resolve(res)
				} else if (res.statusCode === 401) {
					console.log("用户未授权")
				} else {
					reject(res)
				}
			}
		})
	})
}

const getUrl = (url) => {
	if (url.indexOf('://') == -1) {
		url = config.BaseUrl + url;
	}
	return url
}

// // get方法
const _get = (url, param = {}, isPermissions) => {
	return http({
		url,
		param,
		isPermissions
	})
}



const _post = (url, param = {}, isPermissions, contentType) => {
	// console.log(url)
	if(url == 'other/getcity.do'){
		param=param;
	}
	if (url != 'user/wxlogin.do' && url != 'other/showsharereport.do') {
		param.token = uni.getStorageSync('token'),
		param.userid = uni.getStorageSync('uid')
	}
	if(url == 'other/showsharereport.do'){
		param=param;
	}
	
	console.log('param', param)
	return http({
		url,
		param,
		method: 'POST',
		isPermissions,
		contentType
	})
}

const _put = (url, param = {}, isPermissions) => {
	return http({
		url,
		param,
		method: 'put',
		isPermissions
	})
}

const _delete = (url, param = {}, isPermissions) => {
	return http({
		url,
		param,
		method: 'delete',
		isPermissions
	})
}


//微信登录
const _login = (code,encrypteddata, iv) => {
	return new Promise((resolve, reject) => {
		if(code){
			let obj={
				code:code
			}
			if(encrypteddata && iv){
				obj.encrypteddata=encrypteddata;
				obj.iv=iv;
			}
			console.log('obj1',obj)
			_post('user/wxlogin.do', obj).then(res => {
				resolve(res)
			}).catch(err => {
				reject(res)
			})
			return
		}
		wx.login({
			success: res => {
				let code = res.code;
				console.log("用户登录")
				console.log('code', code)
				let obj={
					code:code
				}
				console.log('obj',obj)
				_post('user/wxlogin.do', obj).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})

			}
		})
	})

}
//isPermissions 是否需要权限

const isMssage = (text) => {
	uni.showToast({
		title: text,
		icon: 'none',
		duration: 2000
	})
}
const json2Form = (json) => {
	var str = [];
	for (var p in json) {
		str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
	}
	return str.join("&");
}

export default {
	_get,
	_post,
	_put,
	_delete,
	_login,
	isMssage,
	json2Form
}
